//
//  StatusCell.swift
//  weibo
//
//  Created by apple on 15/12/19.
//  Copyright © 2015年 LYR. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    //  定义一个 底部视图的顶部约束（判断是否有转发微博，更改底部视图的约束）
    var bottomViewTopConstraints: Constraint?

    //MARK:定义外部模型属性
     var status: Status? {
    
        didSet {
          //给顶部视图 设置模型数据
           topView.status = status
            
            // 卸载掉顶部约束
            bottomViewTopConstraints?.uninstall()
            
           //  根据微博模型  提供提供的数据 判断是否有转发微博
            if let retweetedStatus = status?.retweeted_status {
              // 有转发微博  修改约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                
                //  给转发微博的 retweeted_status 模型赋值
                retweetedView.retweeted_status = retweetedStatus
                //  显示转发微博
                retweetedView.hidden = false
                
            
            }else {
              // 原创微博  修改约束
            bottomView.snp_updateConstraints(closure: { (make) -> Void in
                self.bottomViewTopConstraints = make.top.equalTo(topView.snp_bottom).constraint
            })
                // 隐藏转发微博的视图
            retweetedView.hidden = true
            
            }
            
        }
    }
    
    
    //MARK:重写构造方法  (cell 的入口)
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
//        backgroundColor = UIColor(white: 0.9, alpha: 1)
        
        setupUI()
        
        // 设置cell 选中样式（选中的时候不变色）
        self.selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK:设置页面 布局
    private func setupUI() {
    
        contentView.addSubview(topView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)

        //顶部视图的约束
        topView.snp_makeConstraints { (make) -> Void in
            
            make.left.top.right.equalTo(contentView)
            
//            make.height.equalTo(50)
            
            
        }
        
        // 设置转发微博的约束
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(topView.snp_bottom)
           
            //  调试用的高度
//            make.height.equalTo(100)
        }
        
        //  底部视图的约束
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            
//            make.top.equalTo(topView.snp_bottom)
        //    make.top.equalTo(retweetedView.snp_bottom)
          
            self.bottomViewTopConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
            
            make.height.equalTo(40)
            
        }
        
        
        
        // 对contentView 添加约束 以便自动计算行高
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
        
        
    
    }
    
    // MARK:懒加载所有的子视图
    //  顶部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    //  底部视图
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    
    //  转发微博
    private lazy var retweetedView: StatusCellRetweetedView = StatusCellRetweetedView()
   
    
}
